package com.chaoge.web.controller.system;


import com.chaoge.domain.system.Dept;
import com.chaoge.utils.BeanUtil;
import com.chaoge.web.controller.BaseServlet;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/dept")
public class DeptServlet extends BaseServlet {
    //private DeptService deptService=new DeptServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        switch (operation){
            case "list":
                this.list(request,response);
                break;
            case "toAdd":
                this.toAdd(request,response);
                break;
            case "save":
                this.save(request,response);
                break;
            case "toEdit":
                this.toEdit(request,response);
                break;
            case "edit":
                this.edit(request,response);
                break;
            case "delete":
                this.delete(request,response);
                break;
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取数据
        Dept dept= BeanUtil.fillBean(request, Dept.class);
        //将数据传到deptService层
        deptService.delete(dept);
        //跳转到List页面
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取数据
        Dept dept= BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
        //将数据传到deptService层
        deptService.update(dept);
        //跳转到List页面
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException  {
        //获取id
        String id = request.getParameter("id");
        //查询单个数据
        Dept dept = deptService.findById(id);
        //加载页面信息
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);
        //设置数据
        request.setAttribute("dept",dept);
        //页面跳转
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(request,response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        //获取数据
        Dept dept= BeanUtil.fillBean(request,Dept.class,"yyyy-MM-dd");
        //将数据传到deptService层
        deptService.save(dept);
        //跳转到List页面
        response.sendRedirect(request.getContextPath()+"/system/dept?operation=list");
    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
        //加载页面信息
        List<Dept> all = deptService.findAll();
        request.setAttribute("deptList",all);
        //页面跳转
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/add.jsp").forward(request,response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
        int page=1;
        int size=5;
        if (StringUtils.isNotBlank(request.getParameter("page"))){
            page=Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))){
            size=Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = deptService.findAll(page, size);
        //将数据保存
        request.setAttribute("page",all);
        //页面跳转
        request.getRequestDispatcher("/WEB-INF/pages/system/dept/list.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
